package com.api.syh56.shengyunhui.controller.admin;

import com.api.syh56.shengyunhui.common.CommonPage;
import com.api.syh56.shengyunhui.common.CommonResult;
import com.api.syh56.shengyunhui.common.Constants;
import com.api.syh56.shengyunhui.entity.adminEntity.AdminContact;
import com.api.syh56.shengyunhui.entity.dto.WaybillUpdateDTO;
import com.api.syh56.shengyunhui.entity.vo.WAGParamVO;
import com.api.syh56.shengyunhui.entity.adminEntity.WaybillRoute;
import com.api.syh56.shengyunhui.entity.vo.WaybillDetailVO;
import com.api.syh56.shengyunhui.service.*;
import com.api.syh56.shengyunhui.websocket.ChatServer;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 运单管理
 */
@RestController
@RequestMapping("admin/waybill")
public class AdminWayBillController {
    @Autowired
    private AdminWayBillService adminWayBillService;

    @Autowired
    private RoutesService routesService;

    @Autowired
    private WayBillService wayBillService;

    @Autowired
    private AdminContactService adminContactService;

    @Autowired
    private WaybillRouteService waybillRouteService;


    @Autowired
    private ChatServer chatServer;

    /**
     * 查看运单  运单编号/运单状态/发货城市/收货城市/货物名称
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("select")
    public CommonResult<CommonPage<WAGParamVO>> select(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                                        @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
                                                       @RequestParam String waybillNum,
                                                       @RequestParam String waybillStatus,
                                                       @RequestParam String oriCity,
                                                       @RequestParam String desCity,
                                                       @RequestParam String goodsName,
                                                       @RequestParam String shipCompany,
                                                       @RequestParam String driverCompany){
        Page<WAGParamVO> select = adminWayBillService.select(pageNum, pageSize, waybillNum, waybillStatus, oriCity, desCity, goodsName, shipCompany, driverCompany);
        return CommonResult.success(CommonPage.restPage(select));
    }

    /**
     * 删除运单
     * @param id
     * @return
     */
    @PostMapping("delete")
    public CommonResult<Object> delete(@RequestBody Long id){
        adminWayBillService.delete(id);
        return CommonResult.success(null);
    }

    /**
     * 修改运单状态
     * @param waybillUpdateDTO
     * @return
     */
    @PostMapping("update")
    public CommonResult<Object> update(@RequestBody WaybillUpdateDTO waybillUpdateDTO){
        adminWayBillService.updateById(waybillUpdateDTO);
        return CommonResult.success(null);
    }

    /**
     * 获取未确认报价运单
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("getWaybill")
    public CommonResult<Object> getWaybill(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                           @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize){
        Page<WaybillDetailVO> waybill = adminWayBillService.getWaybill(pageNum, pageSize);
        return CommonResult.success(waybill);
    }


    /**
     * 通过运单获取专线用户
     * @param wid
     * @return
     */
    @GetMapping("getRoute")
    public CommonResult<Object> getByWid(@RequestParam Long wid){
//        List<WaybillRoute> waybillRoute = adminWayBillService.getRouteByWid(wid);
        List<WaybillRoute> waybillRoute = adminWayBillService.getRouteByWidN(wid);
        return CommonResult.success(waybillRoute);
    }

    /**
     * 修改联系状态
     * @param adminContact
     * @return
     */
    @PostMapping("updateConStatus")
    public CommonResult<Object> updateConStatus(@RequestBody AdminContact adminContact){
        adminContactService.save(adminContact);
        waybillRouteService.updateContactStatus(adminContact.getUid(), adminContact.getWid(), adminContact.getSid());
        chatServer.onMessage(String.valueOf(Constants.webSocket.UPDATE_CONTACT));
        return CommonResult.success(null);
    }
}
